package NC58_找到搜索二叉树中两个错误的节点;

import java.util.*;

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
}

public class Solution {
    /**
     *
     * @param root TreeNode类 the root
     * @return int整型一维数组
     */
    ArrayList<Integer> inorder = new ArrayList<>();
    public int[] findError (TreeNode root) {
        // write code here
        inorder(root);
        ArrayList<Integer> list = new ArrayList<>(inorder);
        int[] res = new int[2];

        int sign = 0;
        for (int i = 0; i < inorder.size() - 1; i++){
            if (inorder.get(i) > inorder.get(i + 1)) {
                sign++;
                if (sign == 1) {
                    res[1] = inorder.get(i);
                }
                if (sign == 2){
                    res[0] = inorder.get(i + 1);
                    break;
                }
            }
        }


        return res;
    }

    private void inorder(TreeNode root) {
        if (root == null){
            return;
        }
        inorder(root.left);
        inorder.add(root.val);
        inorder(root.right);
    }
}